// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import * as fs from 'fs';
import DependenciesProvider from './DependenciesProvider';

// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {

	// Use the console to output diagnostic information (console.log) and errors (console.error)
	// This line of code will only be executed once when your extension is activated
	console.log('Congratulations, your extension "markdown-create" is now active!');

	// The command has been defined in the package.json file
	// Now provide the implementation of the command with  registerCommand
	// The commandId parameter must match the command field in package.json
	let disposable = vscode.commands.registerCommand('new.mdFile', function (e) {
		// The code you place here will be executed every time your command is executed
		const wsedit = new vscode.WorkspaceEdit();
		// const wsPath = vscode.workspace.workspaceFolders[0].uri.fsPath; // gets the path of the first workspace folder
		const filePath = vscode.Uri.file(e.path + '/world.md');
		vscode.window.showInformationMessage(filePath.toString());
		wsedit.createFile(filePath, {
			ignoreIfExists: true
		});
		vscode.workspace.applyEdit(wsedit);
		vscode.window.showInformationMessage('Created a new file: hello/world.md');
	});

	context.subscriptions.push(disposable);


	let disposableSync = vscode.commands.registerCommand('sync.mdFileName', function (e) {
		console.log(e);
		const wsedit = new vscode.WorkspaceEdit();

		// 获取编辑文件的目录
		let idx = e.fsPath.lastIndexOf('\\');
		let directory = e.fsPath.substring(0, idx);

		// 获取文字标题
		let content = fs.readFileSync(e.fsPath).toString()
		let lineFirst = content.substring(0, content.indexOf('\n'));
		if (lineFirst) {
			lineFirst = lineFirst
				.replace('# ', '') // 标题最开始带空格
				.replace('#', '')
				.replace('\r', '')
				.replace(/[\/\s:]+/g, '-');

			// 如果标题带网络地址
			let match = null;

			if (/\[(.*)\]\(.*\)/.test(lineFirst)) {
				match = lineFirst.match(/\[(.*)\]\(.*\)/);
			} else {
				match = lineFirst.match(/(.*)/);
			}
			if (match && match[1]) {
				console.log('match', match[1]);
				// console.log('lineFirst', lineFirst)
				let res = wsedit.renameFile(vscode.Uri.file(e.fsPath), vscode.Uri.file(directory + '\\' + match[1] + '.md'));
				vscode.workspace.applyEdit(wsedit);
				vscode.window.showInformationMessage('修改标题成功');
			}

		}


		// vscode.window.showInformationMessage('sync.mdFileName');
	});

	context.subscriptions.push(disposableSync);




	// 注册命令
	let disposable2 = vscode.commands.registerCommand('mdc.showOpenEditors', () => {
		// 获取所有打开的编辑器
		const openEditors = vscode.window.visibleTextEditors;

		// 在输出面板中打印编辑器信息
		openEditors.forEach(editor => {
			console.log(`Editor: ${editor.document.fileName}`);
			// console.log(`Editor: ${editor.document.getText()}`);
		});

		// 在消息通知中显示编辑器数量
		vscode.window.showInformationMessage(`Total open editors: ${openEditors.length}`);
	});


	context.subscriptions.push(disposable2);



	vscode.window.registerWebviewViewProvider('helloWorld', new DependenciesProvider(context));
	// 或者
	// vscode.window.createTreeView('package-dependencies', {
	// treeDataProvider: new DependenciesProvider(context),
	// })
}

// This method is called when your extension is deactivated
export function deactivate() { }
